<?php
namespace app\massage\model;

use app\adapay\model\Member;
use app\BaseModel;
use think\facade\Db;

class Commission extends BaseModel
{


    //type 1分销 2加盟商 3技师 4分销商 5上级分销商 6省代分销 7技师拉用户充值余额 8车费 9合伙人 10渠道商 11平台 12业务员
    //定义表名
    protected $name = 'massage_service_order_commission';


    protected $append = [

        'order_goods'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:保留两位小数
     * @author chenniang
     * @DataTime: 2023-05-23 15:29
     */
    public function getCashAttr($value,$data){

        if(isset($value)){

            return round($value,2);
        }
    }


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2023-05-23 15:29
     */
    public function getOrderGoodsAttr($value,$data){

        if(!empty($data['id'])){

            $order_goods_model = new CommissionGoods();

            $list = $order_goods_model->goodsList(['a.commission_id'=>$data['id']]);

            return $list;

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2021-08-25 23:24
     * @功能说明:记录
     */
    public function recordList($dis,$page=10){

        $data = $this->alias('a')
                ->join('massage_service_user_list b','a.user_id = b.id','left')
                ->join('massage_service_user_list c','a.top_id = c.id','left')
                ->join('massage_service_order_list d','a.order_id = d.id','left')
                ->where($dis)
                ->field('a.*,b.nickName,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id,d.car_price,d.material_type,d.material_price,d.discount')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-25 23:24
     * @功能说明:记录
     */
    public function recordListV2($dis,$where,$page=10){

        $data = $this->alias('a')
           // ->join('massage_service_user_list b','a.user_id = b.id','left')
            ->join('massage_service_user_list c','a.top_id = c.id  AND a.type in (1,9)','left')
            ->join('massage_service_order_list d','a.order_id = d.id','left')
            ->join('shequshop_school_admin e','a.top_id = e.id AND a.type in (2,5,6,11)','left')
            ->join('massage_service_coach_list f','a.top_id = f.id AND a.type in (3,8)','left')
            ->join('massage_order_coach_change_logs g','(d.id = g.order_id OR d.add_pid = g.order_id) AND g.is_new = 1 AND a.top_id=0 AND a.type in (3,8) ','left')
            ->join('massage_channel_list h','a.top_id = h.id AND a.type = 10','left')
            ->join('massage_salesman_list m','a.top_id = m.id AND a.type = 12','left')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field('a.*,m.user_name as salesman_name,h.user_name as channel_name,e.agent_name as admin_name,f.coach_name,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id,d.car_price,g.now_coach_name,d.material_price')
            ->group('a.id')
            ->order('d.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-25 23:24
     * @功能说明:记录
     */
    public function recordSelect($dis,$where){

        $data = $this->alias('a')
            ->join('massage_service_user_list c','a.top_id = c.id  AND a.type in (1,9)','left')
            ->join('massage_service_order_list d','a.order_id = d.id')
            ->join('shequshop_school_admin e','a.top_id = e.id AND a.type in (2,5,6,11)','left')
            ->join('massage_service_coach_list f','a.top_id = f.id AND a.type in (3,8)','left')
            ->join('massage_order_coach_change_logs g','(d.id = g.order_id OR d.add_pid = g.order_id) AND g.is_new = 1 AND a.top_id=0 AND a.type in (3,8) ','left')
            ->join('massage_channel_list h','a.top_id = h.id AND a.type = 10','left')
            ->join('massage_salesman_list m','a.top_id = m.id AND a.type = 12','left')
            ->where($dis)
            ->where(function ($query) use ($where){
                $query->whereOr($where);
            })
            ->field('a.*,m.user_name as salesman_name,h.user_name as channel_name,e.agent_name as admin_name,f.coach_name,c.nickName as top_name,d.order_code,d.pay_type,d.pay_price,d.transaction_id,d.car_price,g.now_coach_name,d.material_price')
            ->group('a.id')
            ->order('d.id desc')
            ->select()
            ->toArray();

        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-08-25 23:34
     * @功能说明:佣金到账
     */
    public function successCash($order_id){

        $data = $this->dataInfo(['order_id'=>$order_id,'type'=>1]);

        if(!empty($data)&&$data['status']==1&&$data['cash']>0){

            $user_model = new User();

            $user = $user_model->dataInfo(['id'=>$data['top_id']]);

            $res = $user_model->where(['id'=>$data['top_id'],'new_cash'=>$user['new_cash']])->update(['new_cash'=>$user['new_cash']+$data['cash'],'cash'=>$user['cash']+$data['cash']]);

            if($res==0){

                return $res;
            }

        }

        return 1;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-11-07 16:21
     * @功能说明:代理商以及上级代理佣金到账
     */
    public function adminSuccessCash($order_id){

        $dis = [

            'order_id' => $order_id,

            'status'   => 1
        ];

        $data = $this->where($dis)->where('type','in',[2,5,6])->select()->toArray();

        $admin_model = new Admin();

        if(!empty($data)){

            foreach ($data as $v){

                $cash = $v['cash'];

                $admin = $admin_model->dataInfo(['id'=>$v['admin_id']]);

                if(!empty($admin)&&$cash>0){

                    $res = $admin_model->where(['id'=>$v['admin_id'],'cash'=>$admin['cash']])->update(['cash'=>Db::raw("cash+$cash")]);

                    if($res==0){

                        return $res;
                    }
                }

            }

        }
        //结束所有佣金
        $this->dataUpdate(['order_id'=>$order_id],['status'=>2,'cash_time'=>time()]);

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2023-03-31 14:08
     * @功能说明:佣金到账
     */
    public function commissionSucessCash($order_id){

        $dis = [

            'order_id' => $order_id,

            'status'   => 1
        ];

        $data = $this->where($dis)->select()->toArray();

        $user_model  = new User();

        $admin_model = new Admin();

        $channel_model = new ChannelList();

        $salesman_model= new Salesman();

        $member_model = new Member();

        if(!empty($data)){

            foreach ($data as $v){
                //结束所有佣金
                $res = $this->dataUpdate(['id'=>$v['id']],['status'=>2,'cash_time'=>time()]);

                if($res==0){

                    return $res;
                }

                $cash = $v['cash'];

                if($cash<=0){

                    continue;
                }

                if(in_array($v['type'],[1,9])){
                    //用户分销 合伙人
                    $user = $user_model->dataInfo(['id'=>$v['top_id']]);

                    if(!empty($user)){

                        $res = $user_model->where(['id'=>$v['top_id'],'new_cash'=>$user['new_cash']])->update(['new_cash'=>$user['new_cash']+$cash,'cash'=>$user['cash']+$cash]);

                        if($res==0){

                            return $res;
                        }
                    }

                }elseif (in_array($v['type'],[2,5,6])){
                    //代理商
                    $admin = $admin_model->dataInfo(['id'=>$v['admin_id']]);

                    if(!empty($admin)){

                        $res = $admin_model->where(['id'=>$v['admin_id'],'cash'=>$admin['cash']])->update(['cash'=>$admin['cash']+$cash]);

                        if($res==0){

                            return $res;
                        }
                    }
                }elseif (in_array($v['type'],[10])){
                    //渠道商
                    $channel = $channel_model->dataInfo(['id'=>$v['top_id']]);

                    if(!empty($channel)){

                        $res = $channel_model->where(['id'=>$v['top_id']])->update(['total_cash'=>Db::raw("total_cash+$cash"),'cash'=>Db::raw("cash+$cash")]);

                        if($res==0){

                            return $res;
                        }
                    }

                }elseif (in_array($v['type'],[12])){
                    //业务员
                    $salesman = $salesman_model->dataInfo(['id'=>$v['top_id']]);

                    if(!empty($salesman)){

                        $res = $salesman_model->where(['id'=>$v['top_id']])->update(['total_cash'=>Db::raw("total_cash+$cash"),'cash'=>Db::raw("cash+$cash")]);

                        if($res==0){

                            return $res;
                        }

                    }

                }
                //分账
                $member_model->adapayCommission($v,$order_id);
            }
        }
        return true;

    }




    /**
     * @author chenniang
     * @DataTime: 2022-11-07 16:21
     * @功能说明:渠道商代理佣金到账
     */
    public function channelSuccessCash($order_id){

        $dis = [

            'order_id' => $order_id,

            'status'   => 1
        ];

        $data = $this->where($dis)->where('type','in',[2,5,6])->select()->toArray();

        $admin_model = new Admin();

        if(!empty($data)){

            foreach ($data as $v){

                $cash = $v['cash'];

                $admin = $admin_model->dataInfo(['id'=>$v['admin_id']]);

                if(!empty($admin)&&$cash>0){

                    $res = $admin_model->where(['id'=>$v['admin_id'],'cash'=>$admin['cash']])->update(['cash'=>Db::raw("cash+$cash")]);

                    if($res==0){

                        return $res;
                    }
                }

            }

        }
        //结束所有佣金
        $this->dataUpdate(['order_id'=>$order_id],['status'=>2,'cash_time'=>time()]);

        return true;

    }






    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-26 23:39
     * @功能说明:添加佣金
     */
    public function commissionAdd($order){
        //技师佣金
        $this->coachCommission($order);
        //加盟商佣金
        $this->adminCommission($order);

        $user_model = new User();
        //上级
        $top = $user_model->where(['id'=>$order['user_id']])->value('pid');

        if(!empty($top)){

            $ser_model = new Service();

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            foreach ($order['order_goods'] as $v){
                //查看是否有分销
                $ser = $ser_model->dataInfo(['id'=>$v['goods_id']]);

                if(!empty($ser['com_balance'])){

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'user_id' => $order['user_id'],

                        'top_id'  => $top,

                        'order_id'=> $order['id'],

                        'order_code' => $order['order_code'],

                    ];

                   $find = $com_mdoel->dataInfo($insert);

                   $cash = $v['true_price']*$ser['com_balance']/100*$v['num'];

                   if(empty($find)){

                       $insert['cash'] = $cash;

                       $com_mdoel->dataAdd($insert);

                       $id = $com_mdoel->getLastInsID();

                   }else{

                       $id = $find['id'];

                       $update = [

                           'cash' => $find['cash']+$cash
                       ];
                        //加佣金
                       $com_mdoel->dataUpdate(['id'=>$find['id']],$update);

                   }

                   $insert = [

                       'uniacid' => $order['uniacid'],

                       'order_goods_id' => $v['id'],

                       'commission_id'  => $id,

                       'cash'           => $cash,

                       'num'            => $v['num'],

                       'balance'        => $ser['com_balance']
                   ];
                   //添加到自订单记录表
                   $res = $com_goods_mdoel->dataAdd($insert);

                }

            }

        }

        return true;

    }




    /**
     * @author chenniang
     * @DataTime: 2021-08-26 23:39
     * @功能说明:添加佣金
     */
    public function commissionAddData($order){

        $user_model = new User();

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$order['uniacid']]);

        $dis = [

            'id' => $order['user_id'],

        ];
        //上级
        $top_id = $user_model->where($dis)->value('pid');

        $top = $user_model->dataInfo(['id'=>$top_id]);

        $total_cash = 0;

        if(!empty($top)){

            if($config['fx_check']==1&&$top['is_fx']==0){

                return $total_cash;
            }

            $top = $top['id'];

            $ser_model = new Service();

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            foreach ($order['order_goods'] as $v){
                //查看是否有分销
                $ser = $ser_model->dataInfo(['id'=>$v['goods_id']]);

                $user_agent_balance = 0;

                if($ser['com_balance']<=0){
                    //全局设置
                    $ser['com_balance'] = getConfigSetting($order['uniacid'],'user_agent_balance');

                    $user_agent_balance = 1;
                }

                if(!empty($ser['com_balance'])){

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'user_id' => $order['user_id'],

                        'top_id'  => $top,

                        'order_id'=> $order['id'],

                        'order_code' => $order['order_code'],

                        'balance'    => $user_agent_balance==1?$ser['com_balance']:0,

                    ];

                    $find = $com_mdoel->dataInfo($insert);

                    $cash = $v['true_price']*$ser['com_balance']/100*$v['num'];

                    $total_cash += $cash;

                    if(empty($find)){

                        $insert['cash'] = $cash;

                        $insert['status'] = -1;

                        $insert['admin_id'] = !empty($order['admin_id'])?$order['admin_id']:0;

                        $com_mdoel->dataAdd($insert);

                        $id = $com_mdoel->getLastInsID();

                    }else{

                        $id = $find['id'];

                        $update = [

                            'cash' => $total_cash
                        ];
                        //加佣金
                        $com_mdoel->dataUpdate(['id'=>$find['id']],$update);

                    }

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'order_goods_id' => $v['id'],

                        'commission_id'  => $id,

                        'cash'           => $cash,

                        'num'            => $v['num'],

                        'balance'        => $ser['com_balance']
                    ];
                    //添加到自订单记录表
                    $res = $com_goods_mdoel->dataAdd($insert);

                }

            }

        }

        return $total_cash;

    }



    /**
     * @param $order
     * @功能说明:技师佣金
     * @author chenniang
     * @DataTime: 2022-06-12 13:14
     */
    public function carCommission($order,$status=-1){

        if(isset($order['true_car_price'])&&$order['true_car_price']>0){

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['coach_id'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 8,

                'cash'    => $order['true_car_price'],

                'admin_id'=> $order['admin_id'],

                'balance' => 0,

                'status'  => $status,
            ];

            if(empty($order['coach_id'])){

                $insert['cash_status'] = 0;
            }

            $res = $this->dataAdd($insert);
        }

        return true;
    }

    /**
     * @param $order
     * @功能说明:技师佣金
     * @author chenniang
     * @DataTime: 2022-06-12 13:14
     */
    public function coachCommission($order){

        if(isset($order['coach_cash'])&&$order['coach_cash']>0){

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['coach_id'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 3,

                'cash'    => $order['coach_cash'],

                'admin_id'=> $order['admin_id'],

                'balance' => $order['coach_balance'],

                'status'  => -1,
            ];

            if(empty($order['coach_id'])){

                $insert['cash_status']= 0;
            }

            $res = $this->dataAdd($insert);

            return $res;
        }
        return 0;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-06-12 13:32
     * @功能说明:加盟商佣金
     */
    public function adminCommission($order){

        if(!empty($order['admin_id'])){

            $admin_model = new Admin();

            $city_type = $admin_model->where(['id'=>$order['admin_id']])->value('city_type');

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['admin_id'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 2,

                'cash'    => $order['admin_cash'],

                'admin_id'=> $order['admin_id'],

                'balance' => $order['admin_balance'],

                'status'  => -1,

                'city_type' => $city_type,

            ];
            //如果是线下技师需要把佣金返回给代理商
            if(empty($order['coach_id'])){

                $insert['coach_cash'] = $order['coach_cash'];

                $insert['car_cash']  = $order['true_car_price'];

            }

            $res = $this->dataAdd($insert);
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-06-12 13:32
     * @功能说明:加盟商上级佣金
     */
    public function adminLevelCommission($order){

        if(!empty($order['admin_pid'])){

            $admin_model = new Admin();

            $city_type = $admin_model->where(['id'=>$order['admin_pid']])->value('city_type');

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['admin_pid'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 5,

                'cash'    => $order['level_cash'],

                'admin_id'=> $order['admin_pid'],

                'balance' => $order['level_balance'],

                'status'  => -1,

                'city_type' => $city_type,

            ];

            $res = $this->dataAdd($insert);

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-06-12 13:32
     * @功能说明:省代理商佣金
     */
    public function adminProvinceCommission($order){

        if(!empty($order['p_admin_pid'])){

            $admin_model = new Admin();

            $city_type = $admin_model->where(['id'=>$order['p_admin_pid']])->value('city_type');

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['p_admin_pid'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 6,

                'cash'    => $order['p_level_cash'],

                'admin_id'=> $order['p_admin_pid'],

                'balance' => $order['p_level_balance'],

                'status'  => -1,

                'city_type' => $city_type,

            ];

            $res = $this->dataAdd($insert);

        }

        return true;

    }
    /**
     * @author chenniang
     * @DataTime: 2021-08-28 14:35
     * @功能说明:佣金到账
     */
    public function successCommission($order_id){

        $comm = $this->dataInfo(['order_id'=>$order_id,'status'=>1]);

        if(!empty($comm)){

            $user_model = new User();

            $user = $user_model->dataInfo(['id'=>$comm['top_id']]);

            if(!empty($user)){

                $update = [

                    'balance' => $user['balance']+$comm['cash'],

                    'cash'    => $user['cash']+$comm['cash'],
                ];

                $user_model->dataUpdate(['id'=>$comm['top_id']],$update);

                $this->dataUpdate(['id'=>$comm['id']],['status'=>2,'cash_time'=>time()]);

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-28 14:48
     * @功能说明:退款的时候要减去分销
     */
    public function refundComm($refund_id){

        $refund_model    = new RefundOrder();

        $com_goods_mdoel = new CommissionGoods();

        $order_model     = new Order();

        $refund_order = $refund_model->dataInfo(['id'=>$refund_id]);

        if(!empty($refund_order)){
            //查询这笔等待有无佣金
            $comm = $this->dataInfo(['order_id'=>$refund_order['order_id'],'status'=>1,'type'=>1]);

            if(!empty($comm)){

                foreach ($refund_order['order_goods'] as $v){

                    $comm_goods = $com_goods_mdoel->dataInfo(['commission_id'=>$comm['id'],'order_goods_id'=>$v['order_goods_id']]);

                    if(!empty($comm_goods)){

                        $comm_goods_cash = $comm_goods['cash']/$comm_goods['num'];

                        $true_num = $comm_goods['num'] - $v['num'];

                        $true_num = $true_num>0?$true_num:0;

                        $update = [

                            'num' => $true_num,

                            'cash'=> $comm_goods_cash*$true_num
                        ];

                        $com_goods_mdoel->dataUpdate(['id'=>$comm_goods['id']],$update);
                    }

                }

                $total_cash = $com_goods_mdoel->where(['commission_id'=>$comm['id']])->sum('cash');

                $total_cash = $total_cash>0?$total_cash:0;

                $update = [

                    'cash' => $total_cash,

                    'status' => $total_cash>0?1:-1
                ];

                $this->dataUpdate(['id'=>$comm['id']],$update);

                $order_model->dataUpdate(['id'=>$refund_order['order_id']],['user_cash'=>$total_cash]);

            }

        }

        return true;

    }



    /**
     * @param $v
     * @param $pay_order
     * @功能说明:
     * @author chenniang
     * @DataTime: 2023-03-31 00:35
     */
    public function adminCashCustom($v,$pay_order){

        if($v['type']==2){

            if($v['city_type']==1){

                $pay_order['city_balance'] = $v['balance'];

                $pay_order['admin_balance_name'] = 'city_balance';

                $pay_order['admin_cash_name']    = 'city_cash';
            }else{

                $pay_order['district_balance']   = $v['balance'];

                $pay_order['admin_balance_name'] = 'district_balance';

                $pay_order['admin_cash_name']    = 'district_cash';
            }

        }

        if($v['type']==5){

            $pay_order['city_balance']       = $v['balance'];

            $pay_order['level_balance_name'] = 'city_balance';

            $pay_order['level_cash_name']    = 'city_cash';
        }

        return $pay_order;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-06-13 14:50
     * @功能说明:订单退款修改佣金记录
     */
    public function refundCash($pay_order){

        $order_model = new Order();

        $share_model = new CommShare();

        $level_cash_data = $this->where(['order_id'=>$pay_order['id'],'status'=>1])->where('type','in',[1,2,3,5,6,9,10,11,12])->select()->toArray();
        //查询有无二级市级代理佣金或者省代
        if(!empty($level_cash_data)) {

            foreach ($level_cash_data as $v) {

                $cash_text = $this->getTypeText($v['type']);

                if (in_array($v['type'], [5, 6])) {

                    $pay_order[$cash_text['admin_id']] = $v['admin_id'];
                }

                if (in_array($v['type'], [9, 10, 12])) {

                    $pay_order[$cash_text['admin_id']] = $v['top_id'];

                    $pay_order[$cash_text['coach_share_balance']] = $share_model->getShareBalance($v['id'], 1);

                    $pay_order[$cash_text['admin_share_balance']] = $share_model->getShareBalance($v['id'], 2);

                    $pay_order[$cash_text['company_share_balance']] = $share_model->getShareBalance($v['id'], 3);
                }

                $pay_order[$cash_text['balance']] = $v['balance'];

                $pay_order = $this->adminCashCustom($v, $pay_order);
            }

            //修改佣金信息
            $cash_data = $order_model->getCashData($pay_order,2);

            $cash_data = $cash_data['data'];

            $cash_order_update = [

                'admin_cash'  => $cash_data['admin_cash'],

                'coach_cash'  => $cash_data['coach_cash'],

                'company_cash'=> $cash_data['company_cash'],

                'user_cash'   => $cash_data['user_c_cash'],
            ];

            $arr = [1,2,3,5,6,9,10,11,12];

            foreach ($arr as $value){

                $cash_text = $this->getTypeText($value);

                if(key_exists($cash_text['cash'],$cash_data)){

                    $update = [

                        'cash' => $cash_data[$cash_text['cash']]
                    ];
                    //修改各类佣金记录
                    $this->dataUpdate(['order_id'=>$pay_order['id'],'type'=>$value,'status'=>1],$update);
                    //渠道商 合伙人 业务员 对应的分摊金额可能会变
                    if(in_array($value,[9,10,12])){

                        $comm_data = $this->dataInfo(['order_id'=>$pay_order['id'],'type'=>$value,'status'=>1]);

                        if(!empty($comm_data)){

                            if(key_exists($cash_text['coach_share_cash'],$cash_data)){

                                $share_model->dataUpdate(['comm_id'=>$comm_data['id'],'type'=>1],['share_cash'=>$cash_data[$cash_text['coach_share_cash']]]);
                            }

                            if(key_exists($cash_text['admin_share_cash'],$cash_data)){

                                $share_model->dataUpdate(['comm_id'=>$comm_data['id'],'type'=>2],['share_cash'=>$cash_data[$cash_text['admin_share_cash']]]);
                            }

                            if(key_exists($cash_text['company_share_cash'],$cash_data)){

                                $share_model->dataUpdate(['comm_id'=>$comm_data['id'],'type'=>3],['share_cash'=>$cash_data[$cash_text['company_share_cash']]]);
                            }
                        }
                    }
                }
            }

            $res = $order_model->dataUpdate(['id'=>$pay_order['id']],$cash_order_update);

            return $res;

        }

        return true;

    }


    /**
     * @param $type
     * @功能说明:
     * @author chenniang
     * @DataTime: 2023-02-17 16:05
     */
    public function getTypeText($type){

        switch ($type){

            case 1:
                $arr['cash'] = 'user_c_cash';

                $arr['balance'] = 'user_agent_balance';

                break;

            case 2:
                $arr['cash'] = 'admin_cash';

                $arr['balance']  = 'admin_balance';

                $arr['admin_id'] = 'admin_pid';

                break;
            case 3:
                $arr['cash'] = 'coach_cash';

                $arr['balance'] = 'coach_balance';

                break;

            case 5:
                $arr['cash'] = 'level_cash';

                $arr['balance'] = 'level_balance';

                $arr['admin_id'] = 'admin_pid';

                break;

            case 6:
                $arr['cash'] = 'p_level_cash';

                $arr['balance'] = 'p_level_balance';

                $arr['admin_id'] = 'p_admin_pid';

                break;
            case 9:
                $arr['cash'] = 'partner_cash';

                $arr['balance'] = 'coach_agent_balance';

                $arr['admin_id'] = 'partner_id';

                $arr['coach_share_balance'] = 'partner_coach_balance';

                $arr['admin_share_balance'] = 'partner_admin_balance';

                $arr['company_share_balance']= 'partner_company_balance';

                $arr['coach_share_cash'] = 'partner_coach_cash';

                $arr['admin_share_cash'] = 'partner_admin_cash';

                $arr['company_share_cash']= 'partner_company_cash';

                break;
            case 10:
                $arr['cash'] = 'channel_cash';

                $arr['balance'] = 'channel_balance';

                $arr['admin_id'] = 'channel_id';

                $arr['coach_share_balance'] = 'channel_coach_balance';

                $arr['admin_share_balance'] = 'channel_admin_balance';

                $arr['company_share_balance']= 'channel_company_balance';

                $arr['coach_share_cash'] = 'channel_coach_cash';

                $arr['admin_share_cash'] = 'channel_admin_cash';

                $arr['company_share_cash']= 'channel_company_cash';

                break;

            case 11:
                $arr['cash'] = 'company_cash';

                $arr['balance'] = 'company_balance';

                break;

            case 12:
                $arr['cash'] = 'salesman_cash';

                $arr['balance']  = 'salesman_balance';

                $arr['admin_id'] = 'salesman_id';

                $arr['coach_share_balance']  = 'salesman_coach_balance';

                $arr['admin_share_balance']  = 'salesman_admin_balance';

                $arr['company_share_balance']= 'salesman_company_balance';

                $arr['coach_share_cash']  = 'salesman_coach_cash';

                $arr['admin_share_cash']  = 'salesman_admin_cash';

                $arr['company_share_cash']= 'salesman_company_cash';

                break;
        }

        return $arr;
    }


    /**
     * @author chenniang
     * @DataTime: 2023-03-22 10:12
     * @功能说明:获取佣金相关参数
     */
    public function commissionData($type=1){

        $arr = [
            //技师佣金
             [

                'action_name' => "getCoachCash",

                'parameter'   => 'coach_balance',

            ],//用户分销
            [

                'action_name' => $type==1?'getUserCash':'getUserCashRefund',

                'parameter'   => 'user_agent_balance',
            ],
            //平台抽成
            [

                'action_name' => 'getCompanyCash',

                'parameter'   => 'admin_balance',
            ],
            //省代
            [

                'action_name' => 'getProvinceCash',

                'parameter'   => 'p_level_balance',

            ],//城市代理
            [

                'action_name' => 'getCityCash',

                'parameter'   => 'level_balance',
            ],
            //合伙人
            [

                'action_name' => 'getPartnerCash',

                'parameter'   => 'coach_agent_balance',
            ],
            //渠道商
            [

                'action_name' => 'getChannelCash',

                'parameter'   => 'channel_balance',
            ],
            //业务员
            [

                'action_name' => 'getSalesmanCash',

                'parameter'   => 'salesman_balance',
            ],

        ];

        return $arr;
    }


    /**
     * @param $order
     * @功能说明:计算各类分销比例
     * @author chenniang
     * @DataTime: 2023-03-22 11:52
     */
    public function balanceData($order,$admin_id=0){

        $coach_model = new Coach();

        $admin_model = new Admin();

        $clock_model = new ClockSetting();

        $channel_model = new ChannelList();

        $custom_model  = new CustomBalance();

        $coach = $coach_model->dataInfo(['id'=>$order['coach_id']]);

        $coach_level = $custom_model->getCoachCustomBalance($order['coach_id']);

        if(empty($coach_level)){
            //技师等级
            $coach_level = $coach_model->getCoachLevel($order['coach_id'],$order['uniacid']);

            if(empty($coach_level)&&!empty($order['coach_id'])){

                return ['code'=>300];
            }
        }

        $coach_level['balance'] = !empty($coach_level)?$coach_level['balance']:0;
        //技师佣金比列
        $order['coach_balance'] = $coach_level['balance'];
        //加钟的时候比例可能是特殊设置
        $order['coach_balance'] = $clock_model->getCoachBalance($order);

        $order['admin_id'] = $order['partner_id'] = 0;

        $admin_id = !empty($coach['admin_id'])?$coach['admin_id']:$admin_id;
        //代理商各类分销比例
        $order = $admin_model->agentBalanceData($admin_id,$order);
        //合伙人
        $order = $coach_model->partnerBalance($coach,$order);
        //渠道商|业务员
        $order = $channel_model->channelBalance($order);

        return $order;
    }



    /**
     * @author chenniang
     * @DataTime: 2023-03-22 10:19
     * @功能说明:计算每类佣金的金额
     */

    public function cashData($order,$type){

        $list = $this->commissionData($type);

        if($order['material_type']==1){

            $order['true_service_price'] = $order['true_service_price']+$order['material_price'];
        }

        foreach ($list as $key=>$value){

            $balance = isset($order[$value['parameter']])?$order[$value['parameter']]:0;

            $order['surplus_cash'] = $key==0?$order['true_service_price']:$order['surplus_cash'];

            $action_name = $value['action_name'];

            $order = $this->$action_name($balance,$order,$order['surplus_cash']);

        }

        if($order['material_type']==1){

            $order['true_service_price'] = $order['true_service_price']-$order['material_price'];
        }

        $order['admin_cash'] = $order['surplus_cash'];

        return $order;

    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:合伙人佣金
     * @author chenniang
     * @DataTime: 2023-03-22 11:14
     */
    public function getPartnerCash($balance,$order,$cash){

        if(!empty($order['partner_id'])){

            $order['partner_company_cash'] = 0;
            //合伙人提成
            $order['partner_cash'] = round($balance*$order['true_service_price']/100,2);

            $partner_cash = $order['partner_cash'];
            //技师需要分摊的钱
            $coach_cash = round($partner_cash*$order['partner_coach_balance']/100,2);

            $order['partner_cash'] -= $coach_cash;

            $coach_cash = $coach_cash<$order['coach_cash']?$coach_cash:$order['coach_cash'];

            $order['partner_cash'] += $coach_cash;

            $order['coach_cash']   -= $coach_cash;
            //技师承担金额
            $order['partner_coach_cash'] = $coach_cash;
            //没有代理商就由平台承担
            if(empty($order['admin_id'])){
                //平台承担比例
                $order['partner_company_balance'] = 100>$order['partner_coach_balance']?100-$order['partner_coach_balance']:0;
                //平台需要承担的费用
                $admin_cash = round($partner_cash*$order['partner_company_balance']/100,2);

                $order['partner_cash'] -= $admin_cash;

                $admin_cash = $admin_cash<$order['surplus_cash']?$admin_cash:$order['surplus_cash'];

                $order['partner_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;
                //平台承担金额
                $order['partner_company_cash'] = $admin_cash;

                $order['partner_admin_balance'] = 0;

            }else{

                $admin_cash = round($partner_cash*$order['partner_admin_balance']/100,2);

                $order['partner_cash'] -= $admin_cash;

                $admin_cash = $admin_cash<$order['surplus_cash']?$admin_cash:$order['surplus_cash'];

                $order['partner_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;

                $order['partner_admin_cash'] = $admin_cash;

                $compan_balance = $order['partner_company_balance'];

                $order['partner_admin_id'] = $order['admin_id'];

                if($compan_balance>0){

                    $admin_cash = round($partner_cash*$compan_balance/100,2);

                    $order['partner_cash'] -= $admin_cash;

                    $admin_cash = $admin_cash<$order['company_cash']?$admin_cash:$order['company_cash'];

                    $order['partner_cash'] += $admin_cash;

                    $order['company_cash'] -= $admin_cash;

                    $order['partner_company_cash'] = $admin_cash;
                }
            }
        }
        return $order;
    }




    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:平台
     * @author chenniang
     * @DataTime: 2023-03-22 11:12
     */
    public function getCompanyCash($balance,$order,$cash){

        $order['company_cash'] = round($balance*$order['true_service_price']/100,2);

        $order['company_cash'] = $order['company_cash']>$cash?$cash:$order['company_cash'];

        $order['surplus_cash'] = $cash - $order['company_cash'];

        return $order;
    }

    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:技师佣金
     * @author chenniang
     * @DataTime: 2023-03-21 17:55
     */
    public function getCoachCash($balance,$order,$cash){
        //技师佣金
        $order['coach_cash'] = round($balance*$order['true_service_price']/100,2);

        $order['coach_cash'] = $order['coach_cash']>$cash?$cash:$order['coach_cash'];

        $order['surplus_cash'] = $cash - $order['coach_cash'];
        //第二种物料费规则
        if($order['material_type']==1){

            $order['coach_cash'] = $order['coach_cash']-$order['material_price'];

            $order['coach_cash'] = $order['coach_cash']>0?$order['coach_cash']:0;
        }

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:省代佣金
     * @author chenniang
     * @DataTime: 2023-03-21 18:05
     */
    public function getProvinceCash($balance,$order,$cash){
        //上级代理提成
        $order['p_level_cash'] = round($balance*$order['true_service_price']/100,2);

        $order['p_level_cash'] = $order['p_level_cash'] - $cash>0?$cash:$order['p_level_cash'];

        $order['surplus_cash'] = $cash - $order['p_level_cash'];

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:渠道商佣金
     * @author chenniang
     * @DataTime: 2023-03-21 18:05
     */
    public function getChannelCash($balance,$order,$cash){

        if(!empty($order['channel_id'])){

            $order['channel_company_cash'] = 0;
            //上级代理提成
            $order['channel_cash'] = round($balance*$order['true_service_price']/100,2);

            $channel_cash = $order['channel_cash'];
            //技师需要分摊的钱
            $coach_cash = round($channel_cash*$order['channel_coach_balance']/100,2);

            $order['channel_cash'] -= $coach_cash;

            $coach_cash = $coach_cash<$order['coach_cash']?$coach_cash:$order['coach_cash'];

            $order['channel_cash'] += $coach_cash;

            $order['coach_cash']   -= $coach_cash;
            //技师承担了多少钱
            $order['channel_coach_cash'] = $coach_cash;
            //没有代理商就由平台承担
            if(empty($order['admin_id'])){
                //平台需要承担的费用
                $admin_cash = round($channel_cash*(100-$order['channel_coach_balance'])/100,2);

                $order['channel_cash'] -= $admin_cash;

                $admin_cash = $admin_cash<$order['surplus_cash']?$admin_cash:$order['surplus_cash'];

                $order['channel_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;
                //平台承担佣金
                $order['channel_company_cash'] = $admin_cash;

                $order['channel_company_balance'] = 100-$order['channel_coach_balance'];

                $order['channel_admin_balance'] = 0;

            }else{

                $admin_cash = round($channel_cash*$order['channel_admin_balance']/100,2);

                $order['channel_cash'] -= $admin_cash;
                //上级代理商承担
                if(getConfigSetting($order['uniacid'],'cash_share_admin')==1){

                    $admin_cash = $admin_cash<$order['surplus_cash']?$admin_cash:$order['surplus_cash'];

                    $order['channel_cash'] += $admin_cash;

                    $order['surplus_cash'] -= $admin_cash;

                    $order['channel_admin_id'] = $order['admin_id'];

                }else{
                    //由城市代理商承担
                    if(isset($order['city_admin'])&&isset($order[$order['city_admin']])){

                        $admin_cash = $admin_cash<$order[$order['city_admin']]?$admin_cash:$order[$order['city_admin']];

                        $order['channel_cash'] += $admin_cash;

                        $order[$order['city_admin']] -= $admin_cash;

                        $order['channel_admin_id'] = $order[$order['city_admin_id']];
                    }
                }

                $order['channel_admin_cash'] = $admin_cash;
                //平台承担部分
                $compan_balance = $order['channel_company_balance'];

                if($compan_balance>0){

                    $admin_cash = round($channel_cash*$compan_balance/100,2);

                    $order['channel_cash'] -= $admin_cash;

                    $admin_cash = $admin_cash<$order['company_cash']?$admin_cash:$order['company_cash'];

                    $order['channel_cash'] += $admin_cash;

                    $order['company_cash'] -= $admin_cash;

                    $order['channel_company_cash'] = $admin_cash;
                }

            }

        }

        return $order;
    }


    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:业务员佣金
     * @author chenniang
     * @DataTime: 2023-03-21 18:05
     */
    public function getSalesmanCash($balance,$order,$cash){

        if(!empty($order['salesman_id'])){
            //上级代理提成
            $order['salesman_cash'] = round($balance*$order['true_service_price']/100,2);

            $salesman_cash = $order['salesman_cash'];
            //技师需要分摊的钱
            $coach_cash = round($salesman_cash*$order['salesman_coach_balance']/100,2);

            $order['salesman_company_cash'] = 0;

            $order['salesman_cash'] -= $coach_cash;

            $coach_cash = $coach_cash<$order['coach_cash']?$coach_cash:$order['coach_cash'];
            //技师承担了多少钱
            $order['salesman_coach_cash'] = $coach_cash;

            $order['salesman_cash'] += $coach_cash;

            $order['coach_cash'] -= $coach_cash;
            //没有代理商就由平台承担
            if(empty($order['admin_id'])){
                //平台需要承担的费用
                $admin_cash = round($salesman_cash*(100-$order['salesman_coach_balance'])/100,2);
                //平台承担的比例
                $order['salesman_company_balance'] = 100-$order['salesman_coach_balance'];

                $order['salesman_cash'] -= $admin_cash;

                $admin_cash = $admin_cash<$order['surplus_cash']?$admin_cash:$order['surplus_cash'];
                //平台承担的金额
                $order['salesman_company_cash'] = $admin_cash;

                $order['salesman_cash'] += $admin_cash;

                $order['surplus_cash'] -= $admin_cash;

                $order['salesman_admin_balance'] = 0;

            }else{
                $admin_cash = round($salesman_cash*$order['salesman_admin_balance']/100,2);

                $order['salesman_cash'] -= $admin_cash;
                //上级代理商承担
                if(getConfigSetting($order['uniacid'],'cash_share_admin')==1){

                    $admin_cash = $admin_cash<$order['surplus_cash']?$admin_cash:$order['surplus_cash'];

                    $order['salesman_cash'] += $admin_cash;

                    $order['surplus_cash'] -= $admin_cash;

                    $order['salesman_admin_id'] = $order['admin_id'];

                }else{
                    //由城市代理商承担
                    if(isset($order['city_admin'])&&isset($order[$order['city_admin']])){

                        $admin_cash = $admin_cash<$order[$order['city_admin']]?$admin_cash:$order[$order['city_admin']];

                        $order['salesman_cash'] += $admin_cash;

                        $order[$order['city_admin']] -= $admin_cash;

                        $order['salesman_admin_id'] = $order[$order['city_admin_id']];
                    }

                }
                //代理商承担的金额
                $order['salesman_admin_cash'] = $admin_cash;
                //平台承担部分
                $compan_balance = $order['salesman_company_balance'];

                if($compan_balance>0){

                    $admin_cash = round($salesman_cash*$compan_balance/100,2);

                    $order['salesman_cash'] -= $admin_cash;

                    $admin_cash = $admin_cash<$order['company_cash']?$admin_cash:$order['company_cash'];

                    $order['salesman_cash'] += $admin_cash;

                    $order['company_cash'] -= $admin_cash;
                    //平台
                    $order['salesman_company_cash'] = $admin_cash;
                }

            }

        }

        return $order;
    }


    /**
     * @param $order
     * @param $cash
     * @功能说明:区县佣金
     * @author chenniang
     * @DataTime: 2023-03-21 18:20
     */
    public function getCityCash($balance,$order,$cash){
        //上级代理提成
        $order['level_cash'] = round($balance*$order['true_service_price']/100,2);

        $order['level_cash'] = $order['level_cash'] - $cash>0?$cash:$order['level_cash'];

        $order['surplus_cash'] = $cash - $order['level_cash'];

        return $order;
    }






    /**
     * @param $balance
     * @param $order
     * @param $cash
     * @功能说明:用户分销
     * @author chenniang
     * @DataTime: 2023-03-22 11:09
     */
    public function getUserCash($balance,$order,$cash){

        $user_model   = new User();

        $config_model = new Config();

        $dis = [

            'id' => $order['user_id'],

        ];

        $total_cash = 0;
        //上级
        $top_id = $user_model->where($dis)->value('pid');

        $top = $user_model->dataInfo(['id'=>$top_id]);

        $config = $config_model->dataInfo(['uniacid'=>$order['uniacid']]);

        if(!empty($top)&&$config['fx_check']==1&&$top['is_fx']==0){

            return $order;
        }

        if(!empty($top)){

            $order['user_top_id'] = $top['id'];

            $ser_model  = new Service();

            if(!empty($balance)&&$balance>0){

                $total_cash = $balance*$order['true_service_price'];

            }else{

                foreach ($order['order_goods'] as $v){
                    //查看是否有分销
                    $ser = $ser_model->dataInfo(['id'=>$v['goods_id']]);

                    $ser['com_balance'] = !empty($ser['com_balance'])?$ser['com_balance']:getConfigSetting($order['uniacid'],'user_agent_balance');

                    if($order['material_type']==1){

                        $price = ($v['true_price']+$v['material_price'])*$ser['com_balance']/100*$v['num'];
                    }else{

                        $price = $v['true_price']*$ser['com_balance']/100*$v['num'];
                    }
                    $total_cash += $price;
                }
            }

        }

        $order['user_c_cash'] = $total_cash>$cash?$cash:$total_cash;

        $order['surplus_cash'] = $cash - $order['user_c_cash'];

        return $order;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-08-28 14:48
     * @功能说明:退款的时候要减去分销
     */
    public function getUserCashRefund($balance,$order,$cash){

        $refund_model    = new RefundOrder();

        $com_goods_mdoel = new CommissionGoods();

        $order_model     = new Order();

        $refund_order = $refund_model->dataInfo(['id'=>$order['refund_id']]);

        $total_cash   = 0;

        if(!empty($refund_order)){
            //查询这笔等待有无佣金
            $comm = $this->dataInfo(['order_id'=>$refund_order['order_id'],'status'=>1,'type'=>1]);

            if(!empty($comm)){

                foreach ($refund_order['order_goods'] as $v){

                    $comm_goods = $com_goods_mdoel->dataInfo(['commission_id'=>$comm['id'],'order_goods_id'=>$v['order_goods_id']]);

                    if(!empty($comm_goods)){

                        $comm_goods_cash = $comm_goods['cash']/$comm_goods['num'];

                        $true_num = $comm_goods['num'] - $v['num'];

                        $true_num = $true_num>0?$true_num:0;

                        $update = [

                            'num' => $true_num,

                            'cash'=> $comm_goods_cash*$true_num
                        ];

                        $com_goods_mdoel->dataUpdate(['id'=>$comm_goods['id']],$update);
                    }

                }

                $total_cash = $com_goods_mdoel->where(['commission_id'=>$comm['id']])->sum('cash');

                $total_cash = $total_cash>0?$total_cash:0;

//                $update = [
//
//                    'cash' => $total_cash,
//
//                    'status' => $total_cash>0?1:-1
//                ];

              //  $this->dataUpdate(['id'=>$comm['id']],$update);

               // $order_model->dataUpdate(['id'=>$refund_order['order_id']],['user_cash'=>$total_cash]);

            }

        }

        $order['user_c_cash'] = $total_cash>$cash?$cash:$total_cash;

        $order['surplus_cash'] = $cash - $order['user_c_cash'];

        return $order;

    }





    /**
     * @author chenniang
     * @DataTime: 2021-08-26 23:39
     * @功能说明:添加佣金
     */
    public function commissionAddDataV2($order){

        if(!empty($order['user_top_id'])){

            $ser_model = new Service();

            $com_mdoel = new Commission();

            $com_goods_mdoel = new CommissionGoods();

            $top = $order['user_top_id'];

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $top,

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'balance'    => getConfigSetting($order['uniacid'],'user_agent_balance'),

                'cash' => $order['user_c_cash'],

                'status' => -1,

                'admin_id' => !empty($order['admin_id'])?$order['admin_id']:0

            ];

            $com_mdoel->dataAdd($insert);

            $id = $com_mdoel->getLastInsID();

            foreach ($order['order_goods'] as $v){
                //查看是否有分销
                $ser = $ser_model->dataInfo(['id'=>$v['goods_id']]);

                $ser['com_balance'] = $ser['com_balance']>0?$ser['com_balance']:getConfigSetting($order['uniacid'],'user_agent_balance');

                if(!empty($ser['com_balance'])){

                    if($order['material_type']==1){

                        $cash = ($v['true_price']+$v['material_price'])*$ser['com_balance']/100*$v['num'];
                    }else{

                        $cash = $v['true_price']*$ser['com_balance']/100*$v['num'];
                    }

                    $insert = [

                        'uniacid' => $order['uniacid'],

                        'order_goods_id' => $v['id'],

                        'commission_id'  => $id,

                        'cash'           => $cash,

                        'num'            => $v['num'],

                        'balance'        => $ser['com_balance']
                    ];
                    //添加到自订单记录表
                    $res = $com_goods_mdoel->dataAdd($insert);

                }

            }

        }

        return true;

    }




    /**
     * @param $order
     * @功能说明:增加合伙人佣金
     * @author chenniang
     * @DataTime: 2023-03-22 15:55
     */
    public function partnerCommission($order){

        if(!empty($order['partner_id'])&&isset($order['coach_agent_balance'])){

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['partner_id'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 9,

                'cash'    => $order['partner_cash'],

                'admin_id'=> !empty($order['admin_id'])?$order['admin_id']:0,

                'balance' => $order['coach_agent_balance'],

                'status'  => -1,
            ];

            $res = $this->dataAdd($insert);

            $id  = $this->getLastInsID();

            $share_model = new CommShare();
            //添加技师分摊比例
            if(!empty($order['partner_coach_balance'])){

                $share_model->addData($order['uniacid'],$order['partner_coach_balance'],$order['partner_coach_cash'],$id,1,$order['id'],$order['coach_id']);

            }
            //添加代理商分摊比例
            if(!empty($order['partner_admin_balance'])){

                $share_model->addData($order['uniacid'],$order['partner_admin_balance'],$order['partner_admin_cash'],$id,2,$order['id'],$order['partner_admin_id']);

            }
            //添加平台分摊比例
            if(!empty($order['partner_company_balance'])){

                $share_model->addData($order['uniacid'],$order['partner_company_balance'],$order['partner_company_cash'],$id,3,$order['id']);

            }
        }

        return true;

    }



    /**
     * @param $order
     * @功能说明:增加渠道商佣金
     * @author chenniang
     * @DataTime: 2023-03-22 15:55
     */
    public function channelCommission($order){

        if(!empty($order['channel_id'])&&isset($order['channel_balance'])){

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['channel_id'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 10,

                'cash'    => $order['channel_cash'],

                'balance' => $order['channel_balance'],

                'admin_id'=> !empty($order['admin_id'])?$order['admin_id']:0,

                'channel_qr_id'=> !empty($order['channel_qr_id'])?$order['channel_qr_id']:0,

                'status'  => -1,

            ];

            $res = $this->dataAdd($insert);

            $id  = $this->getLastInsID();

            $share_model = new CommShare();
            //添加技师分摊比例
            if(!empty($order['channel_coach_balance'])){

                $share_model->addData($order['uniacid'],$order['channel_coach_balance'],$order['channel_coach_cash'],$id,1,$order['id'],$order['coach_id']);

            }
            //添加代理商分摊比例
            if(!empty($order['channel_admin_balance'])){

                $share_model->addData($order['uniacid'],$order['channel_admin_balance'],$order['channel_admin_cash'],$id,2,$order['id'],$order['channel_admin_id']);

            }
            //添加平台分摊比例
            if(!empty($order['channel_company_balance'])){

                $share_model->addData($order['uniacid'],$order['channel_company_balance'],$order['channel_company_cash'],$id,3,$order['id']);

            }

        }

        return true;

    }


    /**
     * @param $order
     * @功能说明:增加渠道商佣金
     * @author chenniang
     * @DataTime: 2023-03-22 15:55
     */
    public function salesmanCommission($order){

        if(!empty($order['salesman_id'])&&isset($order['salesman_balance'])){

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => $order['salesman_id'],

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 12,

                'cash'    => $order['salesman_cash'],

                'balance' => $order['salesman_balance'],

                'admin_id'=> !empty($order['admin_id'])?$order['admin_id']:0,

                'status'  => -1,

            ];

            $res = $this->dataAdd($insert);

            $id  = $this->getLastInsID();

            $share_model = new CommShare();
            //添加技师分摊比例
            if(!empty($order['salesman_coach_balance'])){

                $share_model->addData($order['uniacid'],$order['salesman_coach_balance'],$order['salesman_coach_cash'],$id,1,$order['id'],$order['coach_id']);

            }
            //添加代理商分摊比例
            if(!empty($order['salesman_admin_balance'])){

                $share_model->addData($order['uniacid'],$order['salesman_admin_balance'],$order['salesman_admin_cash'],$id,2,$order['id'],$order['salesman_admin_id']);

            }
            //添加平台分摊比例
            if(!empty($order['salesman_company_balance'])){

                $share_model->addData($order['uniacid'],$order['salesman_company_balance'],$order['salesman_company_cash'],$id,3,$order['id']);

            }

        }

        return true;

    }



    /**
     * @param $order
     * @功能说明:增加平台佣金
     * @author chenniang
     * @DataTime: 2023-03-22 15:55
     */
    public function companyCommission($order){

        if(!empty($order['company_cash'])){

            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'top_id'  => 0,

                'order_id'=> $order['id'],

                'order_code' => $order['order_code'],

                'type'    => 11,

                'cash'    => $order['company_cash'],

                'balance' => $order['company_balance'],

                'status'  => -1,

            ];

            $res = $this->dataAdd($insert);

        }

        return true;


    }


    /**
     * @param $order_id
     * @param $order
     * @功能说明:
     * @author chenniang
     * @DataTime: 2023-05-31 13:58
     */
    public function getFinanceText($order){

        $order_id = array_column($order['data'],'order_id');

        $dis[] = ['order_id','in',$order_id];

        $dis[] = ['status','>',-1];

        $list = $this->where($dis)->select()->toArray();

        $dataPath = APP_PATH  . 'massage/info/FinanceText.php' ;

        $text =  include $dataPath ;

        $change_model = new CoachChangeLog();

        if(!empty($list)){

            foreach ($order['data'] as &$values){
                //总的真实的支付金额包含车费
                $values['remain_cash'] = $values['true_service_price'];

                foreach ($text as $vv){

                    $values[$vv['cash']] = 0;

                    $values[$vv['name']] = '';

                }
                foreach ($list as $value){

                    if($values['order_id'] == $value['order_id']){

                        foreach ($text as $vs){

                            if(in_array($value['type'],$vs['type'])){

                                if(empty($vs['city_type'])||(!empty($vs['city_type'])&&$value['city_type']==$vs['city_type'])){

                                    $values[$vs['cash']] = !empty($value)?$value['cash']:0;

                                    $values[$vs['name']] = Db::name($vs['table'])->where(['id'=>$value['top_id']])->value($vs['title']);
                                    //佣金对应者名称
                                    $values[$vs['name']] = !empty($values[$vs['name']])?$values[$vs['name']]:'';
                                    //车费不减
                                    if($value['type']!=8){

                                        $values['remain_cash'] -= $values[$vs['cash']];
                                        //加上代理商分摊的技师金额
                                        $values['remain_cash'] += $value['coach_cash'];
                                    }

                                }

                            }

                        }

                    }

                }

                $values['remain_cash'] = round($values['remain_cash'],2);
                //线下技师
                if(empty($values['coach_id'])){

                    $values['coach_name'] = $change_model->getCoachByOrder($values['order_id']);
                }
            }
        }

        return $order;
    }


























}